package com.example.mall.context;

import com.alibaba.fastjson.JSON;
import com.example.mall.domain.Login;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public class LoginContext {

    //获取用户信息
    public static Login getLogin(){
        try {
            SecurityContext context = SecurityContextHolder.getContext();

            if(context == null){
                return null;
            }

            Authentication authentication = context.getAuthentication();

            if(authentication == null){
                return null;
            }

            return JSON.parseObject((String) authentication.getPrincipal(), Login.class);

        }catch (Exception e){
            log.error("用户登录对象获取失败");
            e.printStackTrace();
            return null;
        }
    }
}
